package net.lcheng.manage.controller;

import net.lcheng.commons.utils.PageList;
import net.lcheng.manage.utils.ControllerUtils;
import net.lcheng.service.PProductService;
import net.lcheng.service.SysPermissionsService;
import net.lcheng.service.WxShopService;
import net.lcheng.vo.PProductModel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;

/**
 * Created by zwr on 2016/7/6 0006.
 */
@Controller
public class CommodityManagementController {
    @Autowired
    private SysPermissionsService permissionsService;
    @Autowired
    private PProductService pProductService;
    @Autowired
    private WxShopService wxShopService;
    private final int PAGESIZE = 10;
    /**商品查询*/
    @RequiresPermissions("sale:management_list")
    @RequestMapping(value = "/commodity/management_list",method = RequestMethod.GET)
    public String list(Model model, @RequestParam(defaultValue = "1", required = false) int page,HttpServletRequest request){
        ControllerUtils.common(model,  permissionsService,"sale:management_list");
        String productName = request.getParameter("productName");
        String shopName = request.getParameter("shopName");
        String stime = request.getParameter("stime");
        String etime = request.getParameter("etime");
        Date sdate = null;
        Date edate = null;
        try {
            sdate = Date.valueOf(stime);
            edate = Date.valueOf(etime);
        } catch (Exception ex) {
        }
        PageList<PProductModel> pProducts = pProductService.getPProductList(page, PAGESIZE, productName,shopName, sdate, edate);
        model.addAttribute("productName", productName);
        model.addAttribute("shopName", shopName);
        model.addAttribute("stime", stime);
        model.addAttribute("etime", etime);
        model.addAttribute("list", pProducts.list);
        model.addAttribute("pageIndex", page);
        model.addAttribute("totalPage", pProducts.totalPage);
        return "commodity_management_list";
    }


    /***
     * 推荐到首页
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sale:recommend")
    @RequestMapping(value = "/commodity/recommend", method = RequestMethod.POST)
    public int openSale(Model model,@RequestParam String id) {
        ControllerUtils.common(model,  permissionsService,"sale:recommend");

        return pProductService.getPProductReIsShopRecommend(id);
    }
    /***
     * 下架经销商品
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sale:status")
    @RequestMapping(value = "/commodity/status", method = RequestMethod.POST)
    public int Status(Model model,@RequestParam String id,@RequestParam Integer status) {
        ControllerUtils.common(model,  permissionsService,"sale:status");
        return pProductService.getPProductByStatus(id,status);
    }
}
